<?php

namespace App\Kuafu\Wechat\Service;

use App\Kuafu\Shop\Service\ShopAppletService;
use App\Kuafu\Wechat\Enum\CodeEnum;
use App\Kuafu\Wechat\Enum\WechatCodeEnum;
use App\Kuafu\Wechat\Exceptions\WechatException;
use App\Library\FeiShu\SendFs;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Kuafu\Base\Service\BaseService;
use GuzzleHttp\Client as GuzzleClient;
use App\Kuafu\Order\Enum\CodeEnum as OrderCodeEnum;

class WechatCallbackBaseService
{
    public string $fromUserName = '';
    public string $toUsername   = '';
    public array  $postData     = [];

    public function __construct(array $postObj)
    {
        $this->postData     = $postObj;
        $this->fromUserName = $postObj['FromUserName'];
        $this->toUsername   = $postObj['ToUserName'];
    }


    /**
     * 微信单发文字消息
     *
     * @param string $content
     * @return void
     */
    public function sendText(string $content)
    {
        if (empty($content)) {
            return;
        }
        Log::channel('wechat')->info('微信单发文字消息', [
            'content' => $content
        ]);
        $data = [
            'touser'  => $this->fromUserName,
            'msgtype' => 'text',
            'text'    => [
                'content' => $content
            ]
        ];
        self::commonSendMsg($data);
    }


    /**
     * 发送图片
     *
     * @param string $mediaId
     * @return void
     */
    public function sendImage(string $mediaId)
    {
        if (empty($mediaId)) {
            return;
        }

        Log::channel('wechat')->info('微信单图片消息', [
            'mediaId' => $mediaId
        ]);

        $data = [
            'touser'  => $this->fromUserName,
            'msgtype' => 'image',
            "image"   =>
                [
                    "media_id" => $mediaId,
                ]
        ];

        self::commonSendMsg($data);
    }

    /**
     * 被动回复 - 文本
     *
     * @param string $toUsername
     * @param string $fromUsername
     * @param string $passivitySendMsg
     * @return void
     */
    public function passivitySendText(string $passivitySendMsg)
    {
        if (empty($passivitySendMsg)) {
            return;
        }
        $time     = time();
        $textTpl  = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                    </xml>";
        $response = sprintf($textTpl, $this->fromUserName, $this->toUsername, $time, $passivitySendMsg);
        echo $response;
    }

    public function commonSendMsg(array $data)
    {
        $token  = WechatCallbackService::getAccessToken();
        $token  = $token['access_token'];
        $url    = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $token;
        $client = new GuzzleClient();
        $body   = json_encode($data, JSON_UNESCAPED_UNICODE);
        Log::channel('wechat')->info("微信发送消息 入参：" . $body);
        $response = $client->request("POST", $url, ['body' => $body, 'timeout' => 10]);
        $response = $response->getBody()->getContents();
        Log::channel('wechat')->info("微信发送消息 结果：" . $response);
    }
}
